package com.mask.im.business.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.Notify;
import com.mask.im.business.mapper.NotifyMapper;
import com.mask.im.business.model.dto.MailDTO;
import com.mask.im.business.model.dto.NotifyUserDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.NotifyService;
import com.mask.im.business.common.util.BeanCopy;
import com.mask.im.business.common.util.PageUtil;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.mask.im.business.common.constant.CommonConstant.TRUE;

@Service
public class NotifyServiceImpl extends ServiceImpl<NotifyMapper, Notify> implements NotifyService {

	@Override
	public RestResponse<List<NotifyUserDTO>> getUserNotify() {
		Integer userInfoId = MaskIMSecurityUtils.getUserId();
		List<Notify> notifyVOS = this.baseMapper.getUserNotify(userInfoId);
		List<Notify> notifyList = notifyVOS.stream()
				.sorted(Comparator.comparing(Notify::getCreateTime))
				.filter(item -> {
					if (item.getDeleteUser() != null) {
						List<String> deleteUserList = Arrays.asList(item.getDeleteUser().split(","));
						return !deleteUserList.contains(userInfoId.toString());
					}else{
						return true;
					}
				}).collect(Collectors.toList());
		List<NotifyUserDTO> notifyUserDTOList = BeanCopy.listCopy(notifyList, NotifyUserDTO.class);
		return RestResponse.ok(notifyUserDTOList);
	}

	@Override
	public RestResponse<MailDTO> notifyById(Integer id) {
		Notify notify = this.baseMapper.selectById(id);
		MailDTO mailDTO = new MailDTO();
		mailDTO.setId(notify.getId());
		mailDTO.setTitle(notify.getNotifyTitle());
		mailDTO.setCreateTime(notify.getCreateTime());
		mailDTO.setContent(notify.getNotifyContent());
		mailDTO.setIcon(notify.getIcon());
		mailDTO.setIsRead(notify.getIsRead());
		return RestResponse.ok(mailDTO);
	}

	@Override
	public RestResponse<String> readNotify(Integer id) {
		Notify notify = Notify.builder().id(id).isRead(TRUE).build();
		int update = this.baseMapper.updateById(notify);
		if (update>0){
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}

	@Override
	public PageResult<Notify> getAdminNotify(ConditionVO conditionVO) {
		Page<Notify> notifyPage = this.baseMapper.selectPage(new Page<>(PageUtil.getLimitPage(), PageUtil.getSize()),
				new LambdaQueryWrapper<Notify>()
						.eq(Objects.nonNull(conditionVO.getId()),Notify::getId, conditionVO.getId())
						.eq(!StringUtils.isEmpty(conditionVO.getKeywords()),Notify::getNotifyTitle, conditionVO.getKeywords()));
		return PageResult.<Notify>builder().code(20000).total(Math.toIntExact(notifyPage.getTotal()))
				.data(notifyPage.getRecords()).build();
	}

	@Override
	public RestResponse<String> deleteNotify(Integer id) {
		int delete = this.baseMapper.deleteById(id);
		if (delete>0){
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}
}

